package com.shsoft.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hb.dialog.myDialog.MyAlertDialog;
import com.shsoft.colorfulcc.MainActivity;
import com.shsoft.colorfulcc.R;
import com.shsoft.common.Constant;
import com.shsoft.entity.CompositeItem;
import com.shsoft.utils.MessageUtil;

import org.litepal.LitePal;

import java.util.List;

public class CompositeItemAdapter extends RecyclerView.Adapter<CompositeItemAdapter.ViewHolder> {
    private List<CompositeItem> compositeItemList;
    private Context context;
    private MainActivity activity;

    public CompositeItemAdapter(List<CompositeItem> compositeItemList, MainActivity activity) {
        this.compositeItemList = compositeItemList;
        this.activity = activity;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(context==null){
            context = parent.getContext();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.composite_item,parent,false);
        final ViewHolder holder = new ViewHolder(view);
        //绑定事件
        holder.edit.setOnClickListener(v -> {
            CompositeItem item = compositeItemList.get(holder.getAdapterPosition());
            // 打开修改页面
            activity.openCompositeEditFragment(item);
        });
        holder.remove.setOnClickListener(v -> {
            CompositeItem item = compositeItemList.get(holder.getAdapterPosition());
            String mode = "";
            if(item.getLightMode() == Constant.LIGHT_MODE_LONG){
                mode = "常亮";
            }else if(item.getLightMode() == Constant.LIGHT_MODE_HORSE){
                mode = "跑马灯";
            }else if(item.getLightMode() == Constant.LIGHT_MODE_FLOWING_WATER){
                mode = "流水灯";
            }else if(item.getLightMode() == Constant.LIGHT_MODE_BREATHING){
                mode = "呼吸灯";
            }
            activity.myAlertDialog = new MyAlertDialog(activity).builder()
                    .setCancelable(false)
                    .setMsg("删除此" + mode + "步骤？")
                    .setPositiveButton("确定", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            // 删除数据
                            if(item.delete() > 0){
                                MessageUtil.toastShort(activity, "删除成功");
                                activity.updateRecycleView();
                                activity.setRemain();
                            }else {
                                MessageUtil.toastShort(activity, "删除失败");
                            }
                        }
                    }).setNegativeButton("返回", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                        }
                    });
            activity.myAlertDialog.show();
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
        //获取assets文件夹里的字体文件
        Typeface font = Typeface.createFromAsset(context.getAssets(), "iconfont.ttf");
        //给指定的TextView加载字体
        holder.edit.setTypeface(font);
        holder.remove.setTypeface(font);
        //设置数据
        CompositeItem item = compositeItemList.get(position);
        holder.num.setText((position + 1) + ".");
        holder.byteCount.setText(item.getByteCount() + "字节");
        if(item.getLightMode() == Constant.LIGHT_MODE_LONG){
            holder.lightMode.setText("常亮模式");
        }else if(item.getLightMode() == Constant.LIGHT_MODE_HORSE){
            holder.lightMode.setText("跑马灯模式");
        }else if(item.getLightMode() == Constant.LIGHT_MODE_FLOWING_WATER){
            holder.lightMode.setText("流水灯模式");
        }else if(item.getLightMode() == Constant.LIGHT_MODE_BREATHING){
            holder.lightMode.setText("呼吸灯模式");
        }
    }

    @Override
    public int getItemCount() {
        return compositeItemList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        TextView num;
        TextView lightMode;
        TextView edit;
        TextView remove;
        TextView byteCount;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            num = itemView.findViewById(R.id.num);
            lightMode = itemView.findViewById(R.id.lightMode);
            edit = itemView.findViewById(R.id.edit);
            remove = itemView.findViewById(R.id.remove);
            byteCount = itemView.findViewById(R.id.byteCount);
        }
    }
}
